package com.ckv.qcm.model.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.ckv.qcm.model.Trainer;
import com.ckv.qcm.util.Encryption;

public class TrainerDAO extends DAO {

	public Trainer find(long id) {
		Trainer trainer = new Trainer();
		try {
			ResultSet result = this.connection.createStatement().executeQuery(
					"SELECT * FROM Formateurs WHERE idFormateur = " + id);
			if (result.next())
				trainer = new Trainer(id, result.getString("Nom"),
						result.getString("Prenom"), result.getString("mailAddr"),
						result.getString("Password"));

		} catch (SQLException e) {
			e.printStackTrace();
		}
		return trainer;

	}

	public ArrayList<Trainer> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	public Trainer create(Trainer obj) {
		// TODO Auto-generated method stub
		return null;
	}

	public Trainer update(Trainer obj) {
		// TODO Auto-generated method stub
		return null;
	}

	public void delete(Trainer obj) {
		// TODO Auto-generated method stub

	}

	public Trainer authentify(Trainer trainer) throws Exception {
		try {
			ResultSet result = this.connection.createStatement().executeQuery(
					"SELECT * FROM Formateur WHERE Mail = '"
							+ trainer.getMail() + "'");
			if (result.next()) {
				if (result.getString("Password").equals(
						Encryption.toSHA(trainer.getPassword()))) {
					return new Trainer(result.getLong("idFormateur"),
							result.getString("Nom"),
							result.getString("Prenom"),
							result.getString("Mail"),
							result.getString("Password"));
				}
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

}
